1. Types of Tasks
You can perform several
broad categories of tasks with the Visual Basic language. Since we're
talking about Visual Basic (not Excel), these tasks tend to be very
general. More specific tasks are usually handled through Excel objects.
Table 1. An overview of Visual Basic programming tasks
Category | Section | Overview |
---|
Users | Interact with Users | Use simple dialog boxes to get or display information. |
Data | Do Math | Perform calculations. |
| Work with Text | Compose and modify strings of text. |
| Get Dates and Times | Get current dates and times and perform calculations on dates and times. |
Storage | Read and Write Files | Open, read, write, and close files stored on disk. |
Expressions | Check Results | Find what kind of data was returned by an operation. |
| Find Truth | Combine expressions to create complex conditions. |
| Compare Bits | Get multiple pieces of information from a single value. |
Interoperate | Run Other Applications | Exchange data with other Windows applications. |
Compiler | Control the Compiler | Create debug and release versions of code within the same source file. |
2. Interact with Users
Visual Basic provides two simple ways to interact with users:
I've already used InputBox and MsgBox a number of times in previous examples, but one more sample won't hurt:
Sub MsgBoxInputBox( )
Dim str As String, val As VbMsgBoxResult
' InputBox gets simple text input.
str = InputBox("Enter some text.", "Chapter 3", "Some text")
' Use If to test if a value was entered.
If str <> "" Then
' You can combine style constants in MsgBox.
val = MsgBox(str, vbQuestion + vbOKCancel, "Chapter 3")
' Return value indicates which button was clicked.
If val = vbOK Then Debug.Print "OK" Else Debug.Print "Cancel"
End If
End Sub
The preceding code displays a simple dialog box to get text, then displays the text in another simple dialog box, as shown in Figure 1.
The MsgBox function can display many different styles and buttons, depending on the Button argument setting. All of the VbMsgBoxStyle settings are listed in Table 2.
Table 2. VbMsgBoxStyle settings
Setting | Description |
---|
Button |
vbOKOnly | Displays OK button. |
vbOKCancel | Displays OK, Cancel buttons. |
vbAbortRetryIgnore | Displays Abort, Retry, Ignore buttons. |
vbYesNoCancel | Displays Yes, No, Cancel buttons. |
vbYesNo | Displays Yes, No buttons. |
vbRetryCancel | Displays Retry, Cancel buttons. |
Icon |
vbCritical | Adds Critical icon (red x). |
vbQuestion | Adds Question icon (?). |
vbExclamation | Adds Exclamation icon (!). |
vbInformation | Adds Information icon (i). |
Default setting |
vbDefaultButton1 | First button is default. |
vbDefaultButton2 | Second button is default. |
vbDefaultButton3 | Third button is default. |
vbDefaultButton4 | Fourth button is default. |
Focus |
vbApplicationModal | Halts workbook until dialog box is closed (this is the default in Excel). |
vbSystemModal | Halts all applications until dialog box is closed. |
vbMsgBoxSetForeground | Displays dialog in the foreground (this is the default in Excel). |
Miscellaneous |
vbMsgBoxHelpButton | Adds a Help button to the dialog. |
vbMsgBoxRight | Right-aligns text (default is left-aligned). |
vbMsgBoxRtlReading | Swaps icon and button positions for right-to-left reading languages such as Arabic. |
Compatible settings in Table 2
can be combined using addition. For instance, you can combine button,
icon, and default settings in a single message box as shown here:
val = MsgBox("Unable to continue.", _
vbCritical + vbAbortRetryIgnore + vbDefaultButton2, "Error")
The value returned by MsgBox is a VbMsgBoxResult constant that indicates which button the user clicked. Typically, you compare that result to the button constants listed in Table 2 in an If or Select statement:
If val = VbMsgBoxResult.vbAbort Then ...
If you are displaying a dialog with only a single OK button, you probably don't care about the value returned by MsgBox. In that case, you can omit the parentheses:
MsgBox "The answer is " & val, , "Chapter 3"
In addition to Visual Basic's built-in InputBox and MsgBox functions, there are several other ways to display much more complex dialog boxes and data-entry forms from Excel (see Table 3).
Table 3. Ways to display complex data-entry forms and dialog boxes
Technique | Use to |
---|
User forms | Create custom dialog boxes or data-entry forms for display from Visual Basic |
Excel's built-in dialogs | Display the standard Excel dialog boxes to get filenames, printer settings, or other common tasks |
InfoPath forms | Collect data in XML format |